package com.example.learnvitamio

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.learnvitamio.base.BaseActivity
import com.example.learnvitamio.databinding.ActivityMediaMetadataBinding
import io.vov.vitamio.MediaMetadataRetriever
import java.io.IOException
import java.lang.IllegalArgumentException
import java.lang.IllegalStateException

class MediaMetadataActivity : AppCompatActivity() {

    private var path:String = "http://82.157.163.217/Monsters.mp3"
    private lateinit var binding:ActivityMediaMetadataBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMediaMetadataBinding.inflate(layoutInflater)
        val mediaMetadataRetriever = MediaMetadataRetriever(this@MediaMetadataActivity)
        try {
            if (path == ""){
                Toast.makeText(this@MediaMetadataActivity,"invalid media path!",Toast.LENGTH_SHORT).show()
            }
            mediaMetadataRetriever.setDataSource(path)
        }catch (e:IllegalArgumentException){
            e.printStackTrace()
        }catch (e:IllegalStateException){
            e.printStackTrace()
        }catch (e:IOException){
            e.printStackTrace()
        }
        val durationMs = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
        val artist = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM_ARTIST)
        val title = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE)
        setContentView(binding.root)
        binding.textView.text = "$durationMs $artist$title"
    }
}